import { Inject, Injectable } from '@nestjs/common';
import { AssignAuthorityDto, CreateAuthorityDto } from './dto/create-authority.dto';
import { UpdateAuthorityDto } from './dto/update-authority.dto';
import { PrismaService } from 'src/prisma/prisma.service';

@Injectable()
export class AuthorityService {

  @Inject()
  private readonly prismaService: PrismaService;

  // 创建权限
  async create(createAuthorityDto: CreateAuthorityDto) {
    const { path, icon, name, key, component, parentId } = createAuthorityDto;
    const authority = await this.prismaService.authority.create({
      data: {
        path,
        icon,
        name,
        key,
        component,
        parentId,
      },
    })
    return authority;
  }

  // 修改权限
  async update(id: string, updateAuthorityDto: UpdateAuthorityDto) {
    const { path, icon, name, component } = updateAuthorityDto;
    const authority = await this.prismaService.authority.update({
      where: {
        id,
      },
      data: {
        path,
        icon,
        name,
        component
      },
    })
    return authority;
  }

  // 分配权限
  async assignAuthority(assignAuthorityDto: AssignAuthorityDto) {

    const { roleId, authorityId } = assignAuthorityDto;

    const roleAuthority = await this.prismaService.roleAuthority.create({
      data: {
        roleId,
        authorityId,
      },
    })
    return roleAuthority;
  }
}
